package com.rxst.msg.config;

import com.rxst.msg.base.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.concurrent.ExecutionException;

/**
 * 全局异常处理
 *
 * @author zgs
 * @date 2020/12/25
 */
@RestControllerAdvice
@Slf4j
@Order(10)
public class CustomExceptionHandler {

    @ExceptionHandler(value = IllegalArgumentException.class)
    public ResponseVo argumentException(IllegalArgumentException e) {
        log.error("系统异常:", e);
        return ResponseVo.error(e.getMessage());
    }

    @ExceptionHandler(value = IllegalStateException.class)
    public ResponseVo illegalException(IllegalStateException e) {
        log.error("系统异常:", e);
        return ResponseVo.error(e.getMessage());
    }

    @ExceptionHandler(value = ExecutionException.class)
    public ResponseVo executionException(ExecutionException e) {
        log.error("系统异常:", e);
        String[] split = e.getMessage().split(":");
        if (split.length > 1) {
            return ResponseVo.error(split[1]);
        }
        return ResponseVo.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseVo handleException(Exception e) {
        log.error("系统异常:", e);
        return ResponseVo.error();
    }
}